package com.freezers.androidclone;



import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;



public class MainScreenActivity extends Activity {

	Button btnViewProducts;
	Button btnNewProduct;
	Button btnImportData;
	Button btnSetSettings;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_screen);
		//
	    //get data from settings activity in this case the language
        //SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
        //in the method getString "date" represents the date from the key value from step 2 and "31/12/2011" represents a default value if the key doesn't exist
        //String userdate = settings.getString("date",null);

        String userdate = settings.getString("date",null);
        String username = settings.getString("username",null);
        String password = settings.getString("password",null);
        String language = settings.getString("language",null);
        String serverurl = settings.getString("serverurl",null);
        
        if(userdate != null){
     	   android.util.Log.e("The Userdate selected is: ",userdate);
        }
        
       if(username != null){
    	   android.util.Log.e("The Username selected is: ",username);
       }
       if(password != null){
    	   android.util.Log.e("The Password selected is: ",password);
       }
       if(language != null){
    	   android.util.Log.e("The Language selected is: ",language);
       }
       if(serverurl != null){
    	   android.util.Log.e("The Server URL selected is: ",serverurl);
       }
      
   
    
		//buttons
		btnViewProducts = (Button) findViewById(R.id.btnViewProducts);
		btnNewProduct = (Button) findViewById(R.id.btnCreateProduct);
		btnImportData = (Button) findViewById(R.id.btnImportData);
		btnSetSettings = (Button) findViewById(R.id.btnSetSettings);
		
		//view products click event
		btnViewProducts.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View view) {
				// TODO Auto-generated method stub
				Intent i = new Intent(getApplicationContext(), AllProductsActivity.class);
				startActivity(i);
			}
		});
		
		//view products click event
		btnNewProduct.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View view) {
				// TODO Auto-generated method stub
				Intent i = new Intent(getApplicationContext(), NewProductActivity.class);
				startActivity(i);
			}
		});
		//view Import All products click event
				btnImportData.setOnClickListener(new View.OnClickListener() {
					
					@Override
					public void onClick(View view) {
						// TODO Auto-generated method stub
						Intent i = new Intent(getApplicationContext(), ImportAllProductsActivity.class);
						startActivity(i);
					}
				});
				
				//view Import All products click event
				btnSetSettings.setOnClickListener(new View.OnClickListener() {
					
					@Override
					public void onClick(View view) {
						// TODO Auto-generated method stub
						Intent i = new Intent(getApplicationContext(), Preferences.class);
						startActivity(i);
					}
				});
	}

	 //Set New main Menu
		public boolean onCreateOptionsMenu(Menu menu){
			super.onCreateOptionsMenu(menu);
			MenuInflater settings = getMenuInflater();
			 settings.inflate(R.menu.main_menu, menu);
			return true; 
			
		}
		public boolean onOptionsItemSelected(MenuItem item){
			
			switch (item.getItemId()){
			case R.id.preferences:
				//new Intent will not get PREFERENCES because of starting from Preferences class
				//addPreferencesFromResource(R.xml.preferences);
				//startActivity(new Intent("com.freezers.androidclone.PREFERENCES"));
				startActivity(new Intent(getApplicationContext(), Preferences.class));
				
				return true;
				
			case R.id.closeApp:
				finish();
				System.exit(0);
				return true;
				
			case R.id.aboutApp:
				
				startActivity(new Intent(getApplicationContext(), AboutApp.class));
				
				return true;
				
			}
				
			return false;
			
		}
		//End Menu
}
